package cn.com.thingo.hyintelligent.ui.activity;


import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.thingo.hyintelligent.R;
import cn.com.thingo.hyintelligent.app.App;
import cn.com.thingo.hyintelligent.base.BaseActivity;
import cn.com.thingo.hyintelligent.base.contract.AuthContract;
import cn.com.thingo.hyintelligent.model.bean.CommonBean;
import cn.com.thingo.hyintelligent.model.bean.GatewayDetailBean;
import cn.com.thingo.hyintelligent.presenter.AuthPresenter;
import cn.com.thingo.hyintelligent.ui.adapter.AuthListAdapter;
import cn.com.thingo.hyintelligent.ui.adapter.decoration.RecycleViewDivider;
import cn.com.thingo.hyintelligent.util.SPUtils;
import cn.com.thingo.hyintelligent.util.ToastUtil;
import cn.com.thingo.hyintelligent.widget.AddAuthDialogFragment;
import cn.com.thingo.hyintelligent.widget.DeleteAuthDialogFragment;

/**
 * 我的授权
 */
public class GatewayAuthActivity extends BaseActivity<AuthPresenter> implements AuthContract.View, AuthListAdapter.OnAuthDeleteListener, AddAuthDialogFragment.CallBack, DeleteAuthDialogFragment.CallBack {
    @BindView(R.id.toolbar_left)
    ImageView ivLeft;
    @BindView(R.id.toolbar_title)
    TextView tvTitle;
    @BindView(R.id.toolbar_right)
    ImageView ivRight;
    @BindView(R.id.auth_list)
    RecyclerView rvList;
    private List<GatewayDetailBean.DataBean.UserBean> list = new ArrayList<>();
    private AuthListAdapter adapter;

    @Override
    protected int getLayout() {
        return R.layout.activity_gateway_auth;
    }

    @Override
    protected void initEventAndData() {
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        mPresenter.gatewayDetail(SPUtils.newInstance().getString(SPUtils.LOGIN_TOKEN), App.getDataBean().getId());
    }

    private void initView() {
        ivLeft.setImageResource(R.mipmap.back);
        tvTitle.setText(R.string.gateway_auth);
        ivRight.setImageResource(R.mipmap.add);
        ivRight.setVisibility(View.VISIBLE);
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        rvList.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.VERTICAL, 2, getResources().getColor(R.color.gray_12)));
        adapter = new AuthListAdapter(list, mContext);
        adapter.setListener(this);
        rvList.setAdapter(adapter);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void viewGatewayDetail(GatewayDetailBean gatewayDetailBean) {
        List<GatewayDetailBean.DataBean.UserBean> user = gatewayDetailBean.getData().getUser();
        list.clear();
        if (user != null) {
            list.addAll(user);
        }
        adapter.notifyDataSetChanged();
    }

    //授权成功的回调
    @Override
    public void viewAddAuth(CommonBean commonBean) {
        ToastUtil.shortShow("授权成功！");
        initData();
    }

    //删除授权的回调
    @Override
    public void viewDelAuth(CommonBean commonBean) {
        ToastUtil.shortShow("已删除授权！");
        initData();
    }

    @OnClick({R.id.toolbar_left, R.id.toolbar_right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.toolbar_left:
                finish();
                break;
            case R.id.toolbar_right:
                AddAuthDialogFragment fragment = new AddAuthDialogFragment(mContext, App.getDataBean().getId());
                fragment.show(getFragmentManager(), "add_auth");
                break;
        }
    }

    //点击删除之后的回调
    @Override
    public void authDelete(int position) {
        DeleteAuthDialogFragment fragment = new DeleteAuthDialogFragment(mContext, position);
        fragment.show(getFragmentManager(), "del_auth");
    }

    //添加授权的弹窗回调
    @Override
    public void determine(String telephone, String gatewayId) {
        mPresenter.addAuth(SPUtils.newInstance().getString(SPUtils.LOGIN_TOKEN), gatewayId, telephone);
    }

    //删除授权的弹窗页面回调
    @Override
    public void determine(int position) {
        GatewayDetailBean.DataBean.UserBean userBean = list.get(position);
        mPresenter.delAuth(SPUtils.newInstance().getString(SPUtils.LOGIN_TOKEN), App.getDataBean().getId(), userBean.getTelephone());

    }
}
